<template>
  <div>
    <crumbs
      @goBack="
        () => {
          $router.push({ path: '/workshopPlanList' })
        }
      "
      :superiorMenu="['计划管理', '车间计划']"
      currentMenu="手动排产"
    />

    <div style="height: calc(100vh - 175px); overflow-y: scroll">
      <div class="wrap">
        <div class="title">基本信息</div>
        <div class="box">
          <div class="boxs" v-for="(item, index) in detailList" :key="index">
            <p class="key">{{ item.key }}</p>
            <p class="value">{{ item.value }}</p>
          </div>
        </div>
      </div>
      <div class="wrap">
        <div class="title">排产明细</div>
        <div style="padding: 20px; text-align: center">
          <j-editable-table
            style="font-size: 14px"
            :ref="editableTable.refKeys"
            :loading="editableTable.loading"
            :columns="editableTable.columns"
            :dataSource="editableTable.dataSource"
          />
        </div>
      </div>
    </div>
    <div class="foot">
      <!-- <div class="save" @click="save">提交</div> -->
      <a-button key="submit" class="save" type="primary" :loading="loadings" @click="save"> 提交 </a-button>
    </div>
  </div>
</template>

<script>
import crumbs from '@/components/crumbs.vue'
import JEditableTable from '@/components/jeecg/JEditableTable.vue'
import { FormTypes } from '@/utils/JEditableTableUtil'
import { getAction, postAction } from '@/api/manage'
import { compareDate } from '@/utils/util'
import moment from 'moment'
export default {
  components: { crumbs, JEditableTable },
  data() {
    return {
      loadings: false,
      editableTable: {
        loading: false,
        refKeys: 'editableTable',
        columns: [
          {
            title: '工序',
            key: 'processName',
          },
          {
            title: '派工方式',
            key: 'dispatchTxt',
          },
          {
            title: '班组',
            key: 'operationTeamId',
            type: FormTypes.select,
            options: [],
            placeholder: '请选择${title}',
            validateRules: [
              {
                required: true, // 必填
                message: '${title}不能为空', // 提示的文本
              },
            ],
          },
          {
            title: '生产设备',
            key: 'deviceId',
            type: FormTypes.select,
            options: [],
            placeholder: '请选择${title}',
          },
          {
            title: '计划开始时间',
            key: 'planStartTime',
            type: FormTypes.datetime,
            width: '20%',
            placeholder: '请选择${title}',
            validateRules: [
              {
                required: true, // 必填
                message: '${title}不能为空', // 提示的文本
              },
            ],
          },
          {
            title: '计划结束时间',
            key: 'planEndTime',
            type: FormTypes.datetime,
            width: '20%',
            placeholder: '请选择${title}',
            validateRules: [
              {
                required: true, // 必填
                message: '${title}不能为空', // 提示的文本
              },
            ],
          },
        ],
        dataSource: [],
      },
      detailList: [],
      formData: {},
    }
  },
  created() {
    console.log(this.editableTable, 'editableTable')

    getAction('/planmgt/erpProductScheduling/manualScheduling', { id: this.$route.query.id }).then((res) => {
      console.log(res, 'detail')
      let e = res.result
      if (res.code === 200) {
        this.detailList = [
          {
            key: '生产订单编号',
            value: e.code || '-',
          },
          {
            key: '生产负责人',
            value: e.chargeUserIdText || '-',
          },
          {
            key: '工艺路线',
            value: e.craftName || '-',
          },
          {
            key: '生产商品编号',
            value: e.goodsCode || '-',
          },
          {
            key: '商品名称',
            value: e.goodsName || '-',
          },
          {
            key: '规格',
            value: e.spec || '-',
          },
          {
            key: '承诺交期',
            value: e.promiseDate || '-',
          },
          {
            key: '计划生产数量',
            value: e.planProductNumUnitText || '-',
          },
        ]
        this.editableTable.dataSource = e.erpProductSchedulingInfoListVo.map((e) => {
          e.dispatchTxt = '派工到班组'
          return e
        })
        this.formData = e
      }
    })
    this.getList()
  },
  methods: {
    getList() {
      getAction('/mess/device/queryOptionList', { pageNo: 1, pageSize: 999, isDelete: 0 }).then((res) => {
        this.editableTable.columns[3].options = res.result.records.map((e) => {
          return {
            title: e.title,
            value: e.value,
          }
        })
      })
      getAction('/mess/teamGroup/queryOptionList', { pageNo: 1, pageSize: 999, isDelete: 0 }).then((res) => {
        this.editableTable.columns[2].options = res.result.records.map((e) => {
          return {
            title: e.title,
            value: e.value,
          }
        })
      })
    },

    save() {
      // 获取所有表单的值，并进行验证
      this.$refs.editableTable.getValues((error, values) => {
        // 错误数 = 0 则代表验证通过
        if (error === 0) {
          // this.$antdMessage.success('验证通过')
          // 将通过后的数组提交到后台或自行进行其他处理
          for (let i = 0; i < values.length; i++) {
            if (compareDate(values[i].planStartTime, values[i].planEndTime)) {
              return this.$antdMessage.error('计划开始时间不能大于计划结束时间')
            }
          }
          let erpProductSchedulingInfoListDto = this.editableTable.dataSource
          for (let i = 0; i < erpProductSchedulingInfoListDto.length; i++) {
            const { deviceId, operationTeamId, planStartTime, processName, planEndTime } = values[i]
            erpProductSchedulingInfoListDto[i].deviceId = deviceId
            erpProductSchedulingInfoListDto[i].operationTeamId = operationTeamId
            erpProductSchedulingInfoListDto[i].planStartTime = planStartTime
            erpProductSchedulingInfoListDto[i].processName = processName
            erpProductSchedulingInfoListDto[i].planEndTime = planEndTime
          }
          let par = {
            erpProductSchedulingInfoListDto,
            goodsId: this.formData.goodsId,
            id: this.formData.id,
            planProductNum: this.formData.planProductNum,
            planProductNumUnit: this.formData.planProductNumUnit,
            processRouteId: this.formData.processRouteId,
          }
          console.log(values, par, this.editableTable.dataSource, 'this.editableTable.dataSource')
          this.loadings = true
          postAction('/planmgt/erpProductScheduling/manualSchedulingSubmit', par).then((res) => {
            console.log(res, '提交')
            if (res.code === 200) {
              this.$antdMessage.success(res.message)
              this.$router.push({ path: '/workshopPlanList' })
            } else {
              this.$antdMessage.error(res.message)
            }
            this.loadings = false
          })
        } else {
          this.$antdMessage.error('请填写必填项')
        }
      })
    },
  },
}
</script>
<style lang="less" scoped>
.wrap {
  background: #fff;
  margin-top: 12px;
  .title {
    font-size: 16px;
    font-weight: bold;
    color: #191f25;
    padding: 15px 20px;
    border-bottom: 1px solid #f5f5f5;
  }
  .box {
    background: #fff;
    border-top: 1px solid #f5f5f5;
    display: flex;
    justify-content: start;
    flex-wrap: wrap;
    padding: 20px 20px 0 20px;
    .boxs {
      width: 33.3%;
      margin-bottom: 25px;
      p {
        margin-bottom: 8px;
        font-size: 14px;
      }
      .key {
        color: #444;
      }
      .value {
        color: #888;
      }
    }
  }
}
.foot {
  width: 100%;
  position: fixed;
  right: -20px;
  bottom: 0;
  background: #fff;
  width: 100vw;
  height: 52px;
  .save {
    // width: 66px;
    height: 32px;
    line-height: 30px;
    background: #1890ff;
    border-radius: 6px;
    color: #fff;
    text-align: center;
    font-size: 14px;
    float: right;
    margin-right: 35px;
    margin-top: 10px;
    cursor: pointer;
  }
}
/deep/.thead {
  background: #fafafa !important;
}
</style>